/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.systemcar.controller;

import javax.faces.bean.ManagedBean;
import br.com.systemcar.model.*;
import java.util.List;
import javax.ejb.EJB;
import br.com.systemcar.dao.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.faces.application.FacesMessage;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;


/**
 *
 * @author danilo
 */
@SessionScoped
@ManagedBean
public class PostoCtrl extends Mensagens implements Serializable {

    private Posto posto = new Posto();
    private List<Combustivel> listaCombustivel;
    private List<PostoCombustivel> listaPostoComustivel;
    private Map<Combustivel, Double> precoCombustivel;
   
    private boolean editar;
    @Inject
    private PostoDao postoDao;
    @Inject
    private PostoCombustivelDao pcd;

    /**
     *
     */
    public PostoCtrl() {
        //if(posto==null)
        //  posto=new Posto();
        // posto.setPstAtivo("S");
        // posto.setPstDatacadastro(new Date());
        listaCombustivel = new ArrayList<Combustivel>();
        listaPostoComustivel = new ArrayList<PostoCombustivel>();
        precoCombustivel = new HashMap<Combustivel, Double>();
        editar = false;
        //  init();


    }

    public void init() {
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("posto") != null) {
            this.posto = (Posto) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("posto");
            setListaPostoComustivel((List<PostoCombustivel>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("listaPosto"));
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("posto");
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("listaPosto");
        }
    }

    public boolean isEditar() {
        return editar;
    }

    public void setEditar(boolean editar) {
        this.editar = editar;
    }

    public List<Posto> listaPosto() {
        Usuario user = (Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        return postoDao.listar(user.getUsrId());
    }

    

    public void addCombustivel(Combustivel cmb) {

        listaCombustivel.add(cmb);


    }

    public void removerCombustivel(Combustivel comb) {
        listaCombustivel.remove(comb);
    }

    public List<PostoCombustivel> getListaPostoComustivel() {
        return listaPostoComustivel;
    }

    public void setListaPostoComustivel(List<PostoCombustivel> listaPostoComustivel) {
        this.listaPostoComustivel = listaPostoComustivel;
    }

    public List<Combustivel> getListaCombustivel() {
        return listaCombustivel;
    }

    public void setListaCombustivel(List<Combustivel> listaCombustivel) {
        this.listaCombustivel = listaCombustivel;
    }

    public Posto getPosto() {
        return posto;
    }

    public void setPosto(Posto posto) {
        this.posto = posto;
    }

//    public void addPreco(double preco,Combustivel comb){
//        List<Combustivel> combustivel =
//        precoCombustivel.put(comb,preco);
//    }
//    
    @PermitAll
    public void addCombustivelPosto(Combustivel combustivel) {
        PostoCombustivel pst = new PostoCombustivel();
        pst.setCombustivel(combustivel);
        // pst.setPosto(posto);
        listaPostoComustivel.add(pst);
    }

    @PermitAll
    public void removerCombustivelPosto(PostoCombustivel pst) {
        listaPostoComustivel.remove(pst);
        if(true){
            pcd.remover(pst);
        }
    }

   

    public void salvar() {
        // PostoCombustivel pc;
        PostoCombustivelPK pk;
        int id;
        Usuario user = (Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");

        posto.setPstAtivo("S");
        posto.setPstDatacadastro(new Date());
        posto.setUsrId(user.getUsrId());
        if(!editar){
        postoDao.salvar(posto);
        }

        for (PostoCombustivel pc : listaPostoComustivel) {
            pk = new PostoCombustivelPK(posto.getPstId(), pc.getCombustivel().getCmbId());
            pc.setPostoCombustivelPK(pk);
            if (!editar) {
                pcd.salvar(pc);
            } else {
                pcd.alterar(pc);
            }
        }

        posto = new Posto();
        listaPostoComustivel = new ArrayList<PostoCombustivel>();
        listaCombustivel = new ArrayList<Combustivel>();
        msgSalvar();

    }

    public void editaPosto(Posto posto) throws IOException {
        this.posto = posto;
        editar = true;
        //  List<PostoCombustivel> lst=pcd.findByPosto(posto.getPstId());
        listaPostoComustivel = pcd.findByPosto(this.posto.getPstId());
//      FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("posto", posto);
//      FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("listaPosto", lst);
        FacesContext.getCurrentInstance().getExternalContext().redirect("/faces/faces/Admin/cadposto.xhtml");
    }

    public void novo() {
        posto = new Posto();
        listaPostoComustivel = new ArrayList<PostoCombustivel>();
        editar = false;
    }
}
